package com.paomi.client.fragment.card.presenter;

import android.app.Activity;

import com.paomi.client.R;
import com.paomi.client.activity.person.model.MyCardBean;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.view.LoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class CardAlreadyUsedPre extends BaseLoadMorePresenter<BaseMvpLCEView> {
    private List<MyCardBean.Data> List = new ArrayList<>();

    private int pageNum=1;
    Activity activity;
    public void getLoadData(Activity activity,int pageNum){
        this.activity=activity;
        this.pageNum=pageNum;
        loadData(true);
    }

    @Override
    protected void loadData(int page, int skip, boolean pullToRefresh) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("assType", "1");
        map.put("pageNum", page + "");
        map.put("pageSize", "10");
        Api.getInstanceService(Constants.User_URL).getCouponList(map).enqueue(new BaseCallback<MyCardBean>() {
            @Override
            public void onResponse(Call<MyCardBean> call, MyCardBean result) {

                if (pageNum == 1) {
                    List.clear();
                }
                if(result.getReturnData().getData()!=null){
                    //比较有重复数据
                    if(List.size()>0 && result.getReturnData().getData().size()>0){
                        boolean isData=false;
                        List<MyCardBean.Data> dataList=result.getReturnData().getData();
                        for (MyCardBean.Data data:dataList){
                            for(int i=0;i<List.size();i++){
                                if(data.getId() == List.get(i).getId()){
                                    isData=false;
                                    break;
                                }else{
                                    isData=true;
                                }
                            }
                        }
                        if(isData){
                            List.addAll(result.getReturnData().getData());
                        }else{
                            ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, activity.getResources().getString(R.string.data_null));
                        }
                    }else{
                        List.addAll(result.getReturnData().getData());
                    }
                }
                getView().setData(List, true);
            }
        });
    }
}
